import React, { Component } from "react";
import "./App.css";

export default class App extends Component {
  state = {
    list: [
      {
        id: `${Math.random()}`.slice(2, 8),
        text: "首页",
      },
      {
        id: `${Math.random()}`.slice(2, 8),
        text: "分类",
      },
      {
        id: `${Math.random()}`.slice(2, 8),
        text: "我的",
      },
    ],
    activeIndex: 0,
  };

  render() {
    return (
      <div>
        <ul>
          {this.state.list.map((item, index) => (
            <li
              key={item.id}
              className={this.state.activeIndex == index ? "active" : ""}
              onClick={this.handleClick(index)}
            >
              {item.text}
            </li>
          ))}
        </ul>
      </div>
    );
  }

  handleClick = (index) => {
    return () => {
      this.setState({
        activeIndex: index,
      });
    };
  };
}
